import axios from '@/utils/axios'

// 文本对比配置接口
export interface ComparisonConfig {
  ignoreCase?: boolean
  ignoreSpace?: boolean
  ignorePunctuation?: boolean
  ignoreEmptyLines?: boolean
  ignoreSymbolDiff?: boolean
  customRegex?: string | null
}

// 字符差异详情
export interface CharacterDifference {
  type: 'DELETE' | 'INSERT'
  startPos1?: number
  endPos1?: number
  startPos2?: number
  endPos2?: number
  content: string
}

// 行差异详情
export interface LineDiff {
  lineNumber: number
  lineNumber2?: number
  status: 'SAME' | 'ADDED' | 'DELETED' | 'MODIFIED'
  content: string
  content2?: string  // MODIFIED 状态时的新内容
  resultIndex?: number
  characterDifferences?: CharacterDifference[]
  contextLine?: boolean
}

// 行统计信息
export interface LineStatistics {
  totalLines: number
  sameLines: number
  modifiedLines: number
  addedLines: number
  deletedLines: number
  emptyLines: number
  lineSimilarity: number
}

// 字符统计信息
export interface CharacterStatistics {
  totalCharacters: number
  sameCharacters: number
  differentCharacters: number
  addedCharacters: number
  deletedCharacters: number
  differenceDensity: number
  characterSimilarity: number
}

// 净字符统计信息
export interface NetCharacterStatistics {
  effectiveCharacters: number
  chineseCharacters: number
  englishCharacters: number
  digitCharacters: number
  otherCharacters: number
  punctuationCharacters: number
  whitespaceCharacters: number
}

// 相似度指标
export interface SimilarityMetrics {
  basicSimilarity: number
  jaccardSimilarity: number
  cosineSimilarity: number
  editDistance: number
  editDistanceSimilarity: number
  weightedSimilarity: number
}

// 详细统计信息
export interface DetailedStatistics {
  lineStatistics: LineStatistics
  characterStatistics: CharacterStatistics
  netCharacterStatistics: NetCharacterStatistics
  similarityMetrics: SimilarityMetrics
}

// 对比结果接口
export interface ComparisonResult {
  text1Title: string
  text2Title: string
  hasDifferences: boolean
  addedLines: number
  deletedLines: number
  similarity: number
  elapsedTime: number
  lineDiffs: LineDiff[]
  detailedStatistics: DetailedStatistics
}

// 对比请求参数
export interface CompareTextParams {
  text1: string
  text2: string
  config?: ComparisonConfig
}

/**
 * 文本对比接口
 * @param params 对比参数
 * @returns 对比结果
 */
export const compareText = async (params: CompareTextParams): Promise<ComparisonResult> => {
  const response = await axios.post('/api/utility/text-comparison/compare', params)
  if (response.code === 200) {
    return response.data
  }
  throw new Error(response.message || '文本对比失败')
}